<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>课程管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="课程名称">
					<el-input v-model="queryInfo.courseName" clearable @clear="queryList"
						placeholder="请输入课程名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddCourse">新增课程</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="courseList" border style="width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID"></el-table-column>
			<el-table-column prop="courseName" label="课程名称">
			</el-table-column>
			<el-table-column prop="tabularChart" label="列表图">
				<template slot-scope='scope'>
					<el-image style="width: 100px; height: 100px"

						:src="'http://121.41.0.164:8888/sfzy/downLoad?filename='+scope.row.tabularChart"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="couseTime" label="课程时长">
			</el-table-column>
			<el-table-column prop="courseIntroduce" label="课程简介">
			</el-table-column>
			<el-table-column prop="applicablePopulation" label="适用人群">
			</el-table-column>
			<el-table-column prop="courseType.name" label="课程分类">
			</el-table-column>
			<el-table-column prop="consult.name" label="咨询师">
			</el-table-column>
			<el-table-column prop="subscriptionNotice" label="订阅须知">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditCourse(scope.row)">编辑课程
					</el-link>
					<el-link type="primary" @click="deleteCourse(scope.row.id)">删除课程</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1,
		5, 10, 20]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
		<!-- 新增和修改的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="course">
				<el-form-item label="课程名称" label-width="80px">
					<el-input placeholder="请输入课程名称" v-model="course.courseName"></el-input>
				</el-form-item>
				<el-form-item label="列表图" label-width="80px">
					<el-upload action="http://10.10.18.194:8888/sfzy/upload" list-type="picture-card"
						:on-success="handleSuccess" :limit="1" :on-preview="handlePictureCardPreview"
						:on-remove="handleRemove" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>
				<el-form-item label="课程时长" label-width="80px">
					<el-input placeholder="请输入课程时长" v-model="course.couseTime"></el-input>
				</el-form-item>
				<el-form-item label="课程简介" label-width="80px">
					<el-input placeholder="请输入课程简介" v-model="course.courseIntroduce"></el-input>
				</el-form-item>
				<el-form-item label="适用人群" label-width="80px">
					<el-input placeholder="请输入适用人群" v-model="course.applicablePopulation"></el-input>
				</el-form-item>
				<el-form-item label="课程分类" label-width="80px">
					<el-select v-model="course.type_id" filterable placeholder="请选择">
						<el-option v-for="item in courseTypeList" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="学习收获" label-width="80px">
					<el-input placeholder="请输入学习收获" v-model="course.learnHarvest"> </el-input>
				</el-form-item>
				<el-form-item label="订阅须知" label-width="80px">
					<el-input placeholder="请输入订阅须知" v-model="course.subscriptionNotice"> </el-input>
				</el-form-item>
				<el-form-item label="咨询师" label-width="80px">
					<el-select v-model="course.teacher_id" filterable placeholder="请选择">
						<el-option v-for="item in consultList" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="课程视频" label-width="80px">

					<el-upload action="http://121.41.0.164:8888/sfzy/upload" list-type="text"
						:on-success="handleVideoSuccess" :limit="1" :on-preview="handleVideoPreview"
						:on-remove="handleVideoRemove" :file-list="videoList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addCourse">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				// 查询信息对象
				queryInfo: {
					courseName: '',
					pageNum: 1,
					pageSize: 10
				},
				total: 183,
				courseList: [],
				title: '', // 对话框标题
				dialogFormVisible: false, // 是否显示对话框
				course: { // 课程对象
					"id": '',
					"courseName": "",
					"tabularChart": "",
					"couseTime": "",
					"courseMount": 0,
					"courseIntroduce": "",
					"applicablePopulation": "",
					"type_id": 3,
					"learnHarvest": "",
					"subscriptionNotice": "",
					"collects": 0,
					"teacher_id": 1,
					"courseVideo": "",
					"pjCount": 0
				},
				dialogVideoUrl: '', // 预览视频路径
				dialogImageUrl: '', // 预览图片路径
				dialogVisible: false, // 预览图片的对话框
				dialogVideoVisible: false, // 预览视频的对话框
				fileList: [],
				videoList: [],
				courseTypeList: [],
				consultList: []
			}
		},
		mounted() {
			this.queryList();
			this.queryCourseType();
			this.queryConsult()
		},
		methods: {
			// 删除文章
			deleteCourse(id) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('Course/delete/' + id)
							.then(resp => {
								if (resp.data.status == 200) {
									this.$message.success(resp.data.message)
									this.queryList()
								} else {
									this.$message.error(resp.data.message)
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
			},

			// 处理上传成功的函数
			handleVideoSuccess(response) {
				// 把上传后的视频路径，交给文章对象的courseVideo
				this.course.courseVideo = response.message;
			},
			// 删除视频的事件
			handleVideoRemove(file, videoList) {
				console.log(file, videoList);
				// 移除数据
				this.course.courseVideo = '';
			},
			// 预览视频事件
			handleVideoPreview(file) {
				this.dialogVideoUrl = file.url;
				this.dialogVideoVisible = true;
			},

			// 处理上传成功的函数
			handleSuccess(response) {
				// 把上传后的图片路径，交给文章对象的tabularChart
				this.course.tabularChart = response.message;
			},
			// 删除图片的事件
			handleRemove(file, fileList) {
				console.log(file, fileList);
				// 移除数据
				this.course.tabularChart = '';
			},
			// 预览图片事件
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			// 进入修改的预览数据页面
			toEditCourse(row) {
				//console.log(row)
				// 赋值
				this.course = row;
				// 设置标题
				this.title = "修改课程";
				// 开启对话框
				this.dialogFormVisible = true;
				// 动态的获取图片的名称并帮到到fileList
				this.fileList = [{
					name: row.tabularChart,
					url: 'http://10.10.18.194:8888/sfzy/downLoad?filename=' + row.tabularChart
				}];
				this.videoList = [{
					name: row.courseVideo,
					url: 'http://10.10.18.194:8888/sfzy/downLoad?filename=' + row.courseVideo
				}];
			},

			cancle() {
				this.dialogFormVisible = false;
				this.course = { // 课程对象
					"id": '',
					"courseName": "",
					"tabularChart": "",
					"couseTime": "",
					"courseMount": 0,
					"courseIntroduce": "",
					"applicablePopulation": "",
					"type_id": 3,
					"learnHarvest": "",
					"subscriptionNotice": "",
					"collects": 0,
					"teacher_id": 1,
					"courseVideo": "",
					"pjCount": 0
				}
				this.fileList = []
				this.videoList = []
			},

			// 添加课程
			addCourse() {
				if (this.course.id) { // 有值为true 修改
					this.$http.put("Course", this.course)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('修改成功！');
								// 清空数据
								this.course = { // 课程对象
									"id": '',
									"courseName": "",
									"tabularChart": "",
									"couseTime": "",
									"courseMount": 0,
									"courseIntroduce": "",
									"applicablePopulation": "",
									"type_id": 3,
									"learnHarvest": "",
									"subscriptionNotice": "",
									"collects": 0,
									"teacher_id": 1,
									"courseVideo": "",
									"pjCount": 0
								}
								this.fileList = []
								this.videoList = []
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				} else {
					this.$http.post("Course", this.course)
						.then(resp => {
							if (resp.data.status == 200) {
								// 消息提示
								this.$message.success('添加成功！');
								// 清空数据
								this.course = { // 课程对象
									"id": '',
									"courseName": "",
									"tabularChart": "",
									"couseTime": "",
									"courseMount": 0,
									"courseIntroduce": "",
									"applicablePopulation": "",
									"type_id": 3,
									"learnHarvest": "",
									"subscriptionNotice": "",
									"collects": 0,
									"teacher_id": 1,
									"courseVideo": "",
									"pjCount": 0
								}
								this.fileList = []
								this.videoList = []
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error('接口异常！')
							}
						})
				}
			},

			// 进入添加的对话框
			toAddCourse() {
				this.title = "新增课程";
				this.dialogFormVisible = true;
			},

			// 分页查询信息的方法
			queryList() {
				// 调用后端接口来获取数据
				this.$http.get('Course/findByPage', {
						params: this.queryInfo
					})
					.then(resp => {
						if (resp.data.status == 200) {
							console.log(resp.data)
							this.courseList = resp.data.data.list;
							this.total = resp.data.data.total;
							//console.log(this.articleList)
						} else {
							this.$message.error('查询失败！')
						}
					})
			},

			// 查询所有的分类信息
			queryCourseType() {
				this.$http.get('coursetype/find').then(res => {
					this.courseTypeList = res.data.data;
					// 以下是处理数据的代码
				})
			},

			// 查询所有的咨询师信息
			queryConsult() {
				this.$http.get('consult/findAll').then(res => {
					this.consultList = res.data.data;
				})
			},

			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.queryList()
			},

			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.queryList();
			}
		},
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>